文档

基于Java ORM框架MyBatis的应用开发

更新时间:

Lindorm宽表引擎提供了一系列基于MySQL协议的连接方式,涉及多种语言和多种框架,其中包括Java对象关系映射(ORM)框架MyBatis。MyBatis框架实现了SQL与代码的解耦,使数据管理变得更加灵活和方便。如果您习惯使用MyBatis框架进行数据开发,或想要对SQL语句进行统一管理和优化,推荐您通过Java ORM框架MyBatis连接和使用Lindorm宽表引擎。

前提条件

  • 已开通MySQL协议兼容功能。如何开通,请参见开通MySQL协议兼容功能

  • 已安装Java环境,要求安装JDK 1.8及以上版本。

  • 已将客户端IP添加至白名单,具体操作请参见设置白名单

操作步骤

  1. 添加MyBatis和MySQL JDBC Driver依赖。以Maven项目为例,在pom.xml文件的dependencies中添加以下依赖项:

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.14</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
  2. resources目录下,创建mybatis-config.xml基础配置文件,用于保存Lindorm宽表引擎相关连接信息。

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "https://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/default"/>
                    <property name="username" value="root"/>
                    <property name="password" value="test"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper class="org.example.UserMapper"/>
        </mappers>
    </configuration>

    参数说明

    参数

    说明

    url

    MySQL协议的Java JDBC连接地址及需要连接的数据库。格式为jdbc:mysql://<MySQL兼容地址>/<数据库名>。默认连接default数据库。

    如何获取MySQL兼容地址,请参见查看连接地址

    重要
    • 如果应用部署在ECS实例,建议您通过专有网络访问Lindorm实例,可获得更高的安全性和更低的网络延迟。

    • 如果应用部署在本地,在通过公网连接Lindorm实例前,需在控制台开通公网地址。开通方式:在控制台选择数据库连接 > 宽表引擎,在宽表引擎页签单击开通公网地址

    • 通过专有网络访问Lindorm实例,url中请填写MySQL兼容地址对应的专有网络地址。通过公网访问Lindorm实例,url中请填写MySQL兼容地址对应的公网地址。

    username

    如果您忘记用户密码,可以通过Lindorm宽表引擎的集群管理系统修改密码。具体操作,请参见修改用户密码

    password

  3. 创建对象类。

    package org.example;
    
    import java.nio.charset.StandardCharsets;
    import java.sql.Date;
    import java.sql.Timestamp;
    
    public class User {
      private int userId;
      private String userName;
      private double height;
      private long score;
      private Timestamp createTime;
      private Date birthday;
    
      private byte[] digest;
    
      public User(int userId, String userName, double height, long score,
          Timestamp createTime, Date birthday, byte[] digest) {
        this.userId = userId;
        this.userName = userName;
        this.height = height;
        this.score = score;
        this.createTime = createTime;
        this.birthday = birthday;
        this.digest = digest;
      }
    
      public int getUserId() {
        return userId;
      }
    
      public void setUserId(int userId) {
        this.userId = userId;
      }
    
      public String getUserName() {
        return userName;
      }
    
      public void setUserName(String userName) {
        this.userName = userName;
      }
    
      public double getHeight() {
        return height;
      }
    
      public void setHeight(double height) {
        this.height = height;
      }
    
      public long getScore() {
        return score;
      }
    
      public void setScore(long score) {
        this.score = score;
      }
    
      public Timestamp getCreateTime() {
        return createTime;
      }
    
      public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
      }
    
      public Date getBirthday() {
        return birthday;
      }
    
      public void setBirthday(Date birthday) {
        this.birthday = birthday;
      }
    
      public byte[] getDigest() {
        return digest;
      }
    
      public void setDigest(byte[] digest) {
        this.digest = digest;
      }
    
      @Override
      public String toString() {
        return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", height=" + height + ", score=" + score + ", createTime=" + createTime + ", birthday=" + birthday + ", digest=" + new String(digest, StandardCharsets.UTF_8) + '}';
      }
    }
  4. 创建MyBatis框架的Mapper,用于定义SQL语句与业务代码的映射关系。

    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    package org.example;
    
    import java.util.List;
    
    public interface UserMapper {
    
      @Update("create table if not exists demo_user(`id` INT, `name` VARCHAR, `height` DOUBLE, `score` BIGINT, `createtime` TIMESTAMP, `birthday` DATE, digest VARBINARY,primary key(id))")
      void createUserTable();
    
      @Update("drop table if exists demo_user")
      void dropUserTable();
    
      @Insert("upsert into demo_user(`id`,`name`,`height`,`score`,`createtime`,`birthday`,`digest`) values(#{userId},#{userName},#{height},#{score},#{createTime},#{birthday},#{digest})")
      int upsertUser(User user);
    
      @Delete("delete from demo_user where `id` = #{userId}")
      int deleteUser(@Param("userId") int userId);
    
      @Select("select * from demo_user where `id` = #{userId}")
      User selectOneUser(@Param("userId") int userId);
    
      @Select("select * from demo_user")
      List<User> selectAllUser();
    }
  5. 编写业务代码。

    package org.example;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    import java.sql.Date;
    import java.sql.Timestamp;
    
    public class MybatisDemo {
      public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(
            inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
          UserMapper mapper = session.getMapper(UserMapper.class);
    
          //create user table
          mapper.createUserTable();
    
          //select all users
          System.out.println(mapper.selectAllUser());
    
          User user1 = new User(1, "zhangsan", 1.8, 100,
              new Timestamp(System.currentTimeMillis()),
              Date.valueOf("1995-03-02"),
              "hello".getBytes(StandardCharsets.UTF_8));
          User user2 = new User(2, "lisi", 1.7, 90,
              new Timestamp(System.currentTimeMillis()),
              Date.valueOf("1996-08-02"),
              "world".getBytes(StandardCharsets.UTF_8));
    
          //insert user1 and user2
          mapper.upsertUser(user1);
          mapper.upsertUser(user2);
    
          //select all users
          System.out.println(mapper.selectAllUser());
          //select user1
          System.out.println(mapper.selectOneUser(1));
    
          //delete user1
          mapper.deleteUser(1);
          System.out.println(mapper.selectAllUser());
    
          //update user2's score to 99
          user2.setScore(99);
          mapper.upsertUser(user2);
          System.out.println(mapper.selectAllUser());
    
          //drop user table
          mapper.dropUserTable();
        }
      }
    }

完整示例

完整示例代码,请参见mybatis-demo.tar

执行成功后,将返回以下结果:

[User{userId=1, userName='zhangsan', height=1.8, score=100, createTime=2023-12-02 09:39:17.63, birthday=1995-03-02, digest=hello}, User{userId=2, userName='lisi', height=1.7, score=90, createTime=2023-12-02 09:39:17.63, birthday=1996-08-02, digest=world}]
User{userId=1, userName='zhangsan', height=1.8, score=100, createTime=2023-12-02 09:39:17.63, birthday=1995-03-02, digest=hello}
[User{userId=2, userName='lisi', height=1.7, score=90, createTime=2023-12-02 09:39:17.63, birthday=1996-08-02, digest=world}]
[User{userId=2, userName='lisi', height=1.7, score=99, createTime=2023-12-02 09:39:17.63, birthday=1996-08-02, digest=world}]
  • 本页导读 (1)
文档反馈